package whf.framework.util;

import java.util.List;

/**
 * @author wanghaifeng
 *
 */
@SuppressWarnings("unchecked")
public class TypeUtils {
	
	public final static Class[] PRIMITIVE_TYPES = {
		int.class, Integer.class, long.class, Long.class, 
		float.class, Float.class, double.class, Double.class, 
		short.class, Short.class, char.class, Character.class, 
		boolean.class, Boolean.class, 
		java.sql.Date.class, java.util.Date.class, java.sql.Timestamp.class,  
		String.class};
	
	private TypeUtils(){
		
	}
	
	public static boolean isNumeric(Class c){
		return c != null && (
			c == Double.class || c == double.class || 
			c == Float.class || c == float.class ||
			c == Integer.class || c == int.class ||
			c == Long.class || c == long.class ||
			c == Short.class || c == short.class
		);
	}
	
	public static boolean isDate(Class c){
		return c != null && (
			c == java.util.Date.class ||
			c == java.sql.Date.class ||
			c == java.sql.Timestamp.class
		);
	}

	
	/**
	 * Struts can convert data type
	 * @param cls
	 * @return
	 */
	public static boolean isPrimitiveType(Class cls){
		for(Class c: PRIMITIVE_TYPES) {
			if(c == cls)
				return true;
		}
		return false;
	}

	
	/**
	 * 判定一个类是否继承指定类
	 * @modify wanghaifeng Oct 16, 2006 2:19:07 PM
	 * @param thisClass 被判定类
	 * @param superClass 指定父类
	 * @return
	 */
	public static boolean isExtendsFrom(Class thisClass, Class superClass){
		return superClass.isAssignableFrom(thisClass);
	}
	
	/**
	 * 判定一个类是否实现了指定的接口
	 * @modify wanghaifeng Oct 16, 2006 2:18:41 PM
	 * @param thisClass 被判定类
	 * @param interfaceClass 接口类名
	 * @return true:实现了;  false:没有
	 */
	public static boolean isImplementsFrom(Class thisClass, Class interfaceClass){
		return interfaceClass.isAssignableFrom(thisClass);
	}
	
	/**
	 * 获取一个类所有的父类以及父类的父类等等...
	 * @modify wanghaifeng Oct 16, 2006 2:20:23 PM
	 * @param thisClass 当前类
	 * @return 父类的集合
	 */
	public static List<Class> getSuperClasses(Class thisClass){
		List<Class> result = Utils.newArrayList();
		Class superClass = thisClass.getSuperclass();
		if(superClass != null){
			if(superClass == Object.class){
				return result;
			} else {
				result.addAll(getSuperClasses(superClass));
				result.add(superClass);
			}
		}
		return result;
	}
	
	/**
	 * 找到一个类的所有实现了的接口
	 * @param thisClass 指定类
	 * @return 所有的接口类名
	 */
	public static List<Class> getImplementsInterfaces(Class thisClass){
		List<Class> result = Utils.newArrayList();
		List<Class> classes = getSuperClasses(thisClass);
		classes.add(thisClass);
		for(Class cls: classes){
			Class[] interfaceClasses = cls.getInterfaces();
			for(Class ic: interfaceClasses){
				result.add(ic);
			}
		}
		return result;
	}
	
	public static void main(String[] args) throws Exception {
		Class cls1 = whf.framework.entity.Entity.class;
		Class cls2 = whf.framework.meta.entity.Object.class;
		System.out.println(cls1.isAssignableFrom(cls2));
	}
}
